﻿using System;
using System.Data.Entity;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormNhanVienCapNhap : DevComponents.DotNetBar.RibbonForm
    {
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        public FormNhanVienCapNhap()
        {
            InitializeComponent();
        }
        public int Id = 0;
        private void txtTenNhanVien_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }
        private void txtChucVu_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }

        private void txtDienThoai_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }

        private void txtDiaChi_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }

        private void txtMaSoThue_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }

        private void txtTaiKhoanNganHang_TextChanged(object sender, EventArgs e)
        {
            KiemTraTrangThai();
        }

        private void BttLuu_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void BttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormNhanVienCapNhap_Load(object sender, EventArgs e)
        {
            loaddata();
        }

        void Edit()
        {
            try
            {
                var t = db.NhanViens.Find(Id);
                t.TenNhanVien = txtTenNhanVien.Text;
                t.ChucVu = txtChucVu.Text;
                t.DienThoai = txtDienThoai.Text;
                t.DiaChi = txtDiaChi.Text;
                t.MaSoThue = txtMaSoThue.Text;
                t.TaiKhoanNganHang = txtTaiKhoanNganHang.Text;
                t.DienGiai = txtDienGiai.Text;
                db.Entry(t).State = EntityState.Modified;
                db.SaveChanges();
                MessageBox.Show("Cập nhập thanh công!");
                this.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void loaddata()
        {
            if (Id != 0)
            {
                var t = db.NhanViens.Find(Id);
                txtDienThoai.Text = t.DienThoai;
                txtChucVu.Text = t.ChucVu;
                txtDiaChi.Text = t.DiaChi;
                txtMaSoThue.Text = t.MaSoThue;
                txtDienGiai.Text = t.DienGiai;
                txtTenNhanVien.Text = t.TenNhanVien;
                txtTaiKhoanNganHang.Text = t.TaiKhoanNganHang;
            }
            else
            {
                MessageBox.Show("Không tải được dư liệu!");
                this.Close();
            }
        }

        void KiemTraTrangThai()
        {
            if (txtDienThoai.Text != "" && txtChucVu.Text != "" && txtDiaChi.Text != "" && txtMaSoThue.Text != "" && txtTenNhanVien.Text != "" && txtTaiKhoanNganHang.Text!="")
            {
                BttLuu.Enabled = true;
            }
            else
            {
                BttLuu.Enabled = false;
            }
        }

        
    }
}
